package org.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.example.bean.User;
import org.example.config.JwtConfig;
import org.example.exception.PasswordNotExistsException;
import org.example.exception.UserNamePasswordException;
import org.example.exception.UserRemoveException;
import org.example.exception.UserSaveException;
import org.example.mapper.UserMapper;
import org.example.service.UserService;
import org.example.util.JsonResult;
import org.example.util.ResultTool;
import org.example.util.StringUtil;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
//    @Override
//    public JsonResult findUser(){
//        JsonResult result = new JsonResult();
//        List<User> list = list();
//        result.setData(list);
//        result.setSuccess(true);
//        result.setCode(200);
//        return result;
//    }
@Resource
private StringRedisTemplate stringRedisTemplate;
    @Override
    public JsonResult findUser(){
        return ResultTool.success(list());
    }

    @Override
    public JsonResult findUser(int page) {
        //之前确认分页信息 page：页码：10：每页显示多少条
        Page<User> p = Page.of(page, 10);
        List<User> list = list(p);
        p.setRecords(list);
        return ResultTool.success(p);
    }
    @Override
    public JsonResult findUserById(String id){
        User one = getById(id);
        return ResultTool.success(one);
    }
    @Override
    public JsonResult findUserByUsername(String username,int page){
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("username",username);
        Page<User> of = Page.of(page, 10);
        List<User> list = list(of, wrapper);
        of.setRecords(list);
        return ResultTool.success(of);
    }
    @Override
    public JsonResult saveUser(User user){
        boolean save=save(user);
        if (!save){
            throw new UserSaveException("添加用户失败");
        }
        return ResultTool.success("success");
    }
    @Override
    public JsonResult updateUser(User user){
        boolean update=updateById(user);
        if (!update){
            throw new UserSaveException("更新用户失败");
        }
        return ResultTool.success("success");
    }
    @Override
    public JsonResult updateUserPassword(String id,String newpass){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("password", StringUtil.encryptMD5(newpass));
        boolean update=update(wrapper);
        if (!update){
            throw new UserSaveException("更新用户失败");
        }
        return ResultTool.success("success");
    }
    @Override
    public JsonResult removeUser(String id){
        boolean remove=removeById(id);
        if (!remove){
            throw new UserSaveException("删除用户失败");
        }
        return ResultTool.success("success");
    }
    @Override
    public JsonResult updateUserStatus(String id,int status){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        boolean update=update(wrapper);
        if (!update){
            throw new UserRemoveException("修改状态失败");
        }
        return ResultTool.success("success");
    }

    @Override
    public JsonResult checkPasswordExists(String id,String password){
        UpdateWrapper<User> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.eq("password",StringUtil.encryptMD5(password));
        User one = getOne(wrapper);
        if (one==null){
            throw new PasswordNotExistsException("你的原始密码错误");
        }
        return ResultTool.success("success");
    }
    @Override
    public JsonResult login(User user) {
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", user.getUsername());
        wrapper.eq("password", user.getPassword());
        User one = getOne(wrapper);
        if (one == null) {
            throw new UserNamePasswordException("账号或者密码错误");
        }
        // 1. 生成token
        String token = JwtConfig.getJwtToken(one);
        // 2. 保存到redis
        ValueOperations<String, String> ops = stringRedisTemplate.opsForValue();
        ops.set("token:" + one.getId(), token);
        // 3. 返回
        return ResultTool.success(token);
    }
}
